using System;
using System.Runtime.CompilerServices;
using Beetle;

internal abstract class r : Package
{
	private new ReadObjectInfo m_a = new ReadObjectInfo();

	private new int b;

	private bool c = true;

	public r()
	{
	}

	public r(aq A_0)
		: base(A_0)
	{
	}

	protected abstract IMessage a(IDataReader A_0, ReadObjectInfo A_1);

	protected abstract void a(IMessage A_0, IDataWriter A_1);

	[SpecialName]
	protected abstract byte[] a();

	public override void MessageWrite(IMessage msg, IDataWriter writer)
	{
		a(msg, writer);
		msg.Save(writer);
		writer.Write(a(), 0, a().Length);
	}

	public override IMessage MessageRead(IDataReader reader)
	{
		IMessage message = a(reader, this.m_a);
		if (message == null)
		{
			throw NetTcpException.TypeNotFound(this.m_a.ToString());
		}
		try
		{
			message.Load(reader);
			return message;
		}
		catch (Exception innerexception)
		{
			NetTcpException ex = NetTcpException.ObjectLoadError(this.m_a.ToString(), innerexception);
			throw ex;
		}
	}

	public override void Import(byte[] data, int start, int count)
	{
		int num = start + count;
		while (count > 0)
		{
			if (c)
			{
				((x)Writer).Reset();
				c = false;
			}
			if (b == 0)
			{
				int num2 = 0;
				if (num2 == -1)
				{
					if (Writer.Length + count > TcpUtils.DataPacketMaxLength)
					{
						throw NetTcpException.DataOverflow();
					}
					Writer.Write(data, start, count);
					int num3 = 0;
					bool flag = false;
					for (int num4 = num - (a().Length - 1); num4 < num; num4++)
					{
						flag = true;
						for (int num5 = 0; num5 < a().Length && num4 + num5 < num; num5++)
						{
							if (data[num4 + num5] != a()[num5])
							{
								flag = false;
							}
						}
						if (flag)
						{
							num3 = num4;
							break;
						}
					}
					if (num3 != 0)
					{
						b = num - num3;
					}
					break;
				}
				if (Writer.Length + count > TcpUtils.DataPacketMaxLength)
				{
					throw NetTcpException.DataOverflow();
				}
				Writer.Write(data, start, count);
				start = num2 + 1;
				count = num - start;
				OnMessageDataReader(Writer);
				b = 0;
				c = true;
				continue;
			}
			bool flag2 = true;
			int num6 = a().Length - b;
			for (int num7 = b; num7 < a().Length; num7++)
			{
				if (data[num7 - b] != a()[num7])
				{
					flag2 = false;
				}
			}
			if (flag2)
			{
				Writer.Write(data, start, num6);
				start += num6;
				count = num - start;
				OnMessageDataReader(Writer);
				c = true;
			}
			b = 0;
		}
	}
}
